package com.mdd.admin.controller.order;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.IOrderMealService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.order.OrderMealCreateValidate;
import com.mdd.admin.validate.order.OrderMealSearchValidate;
import com.mdd.admin.validate.order.OrderMealUpdateValidate;
import com.mdd.admin.vo.order.OrderMealVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.enums.HttpEnum;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 套餐控制层
 * @author shenjian
 * @createTime 2023年02月11日 21:40:00
 */
@RestController
@RequestMapping("api/order_meal")
@CrossOrigin
public class OrderMealController {
    
    @Resource
    IOrderMealService iOrderMealService;

    /**
     * 套餐列表
     *
     * @author shenjian
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return AjaxResult<PageResult<MealVo>>
     */
    @Log(title = "套餐列表")
    @GetMapping("/list")
    public AjaxResult<PageResult<OrderMealVo>> list(@Validated PageValidate pageValidate,
                                                    @Validated OrderMealSearchValidate searchValidate) {
        PageResult<OrderMealVo> list = iOrderMealService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @Log(title = "查询套餐详情")
    @GetMapping("/detail")
    public AjaxResult<OrderMealVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        return AjaxResult.success(iOrderMealService.detail(id));
    }

    @Log(title = "新增套餐")
    @PostMapping("/add")
    public AjaxResult<?> add(@Validated @RequestBody OrderMealCreateValidate createValidate) {
        iOrderMealService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "更新套餐")
    @PostMapping("/edit")
    public AjaxResult<?> update(@Validated @RequestBody OrderMealUpdateValidate updateValidate) {
        iOrderMealService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "删除套餐")
    @GetMapping("/del")
    public AjaxResult<?> delete(@Validated @IDMust() @RequestParam("id") Integer id) {
        iOrderMealService.del(id);
        return AjaxResult.success();
    }

    @Log(title = "创建套餐编号")
    @GetMapping("/create_sn")
    public AjaxResult<String> createSn() {
        return AjaxResult.success(HttpEnum.SUCCESS.getCode(), HttpEnum.SUCCESS.getMsg(), iOrderMealService.createSn());
    }
}
